<template>
  <div>
    <el-dialog
      :title="$t('customer.addGroup')"
      :close-on-click-modal="false"
      width="540px"
      :visible.sync="this.dialogVisible"
      append-to-body
      @open="getInit"
      :before-close="handleClose"
    >
      <el-form
        style="margin: 0px 20px 50px"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item :label="$t('customer.groupName')" prop="name">
          <el-input
            :placeholder="$t('customer.groupName')"
            v-model="ruleForm.name"
            clearable
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">{{ $t('common.cancel') }}</el-button>
        <el-button type="primary" @click="submitAddGroup('ruleForm')">{{
          $t('permission.confirm')
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAction,
  postAction,
  getUrlAction,
  postUrlAction,
} from '@/api/common'
export default {
  name: 'AddGroup',
  props: {
    show: {
      type: Boolean,
      default: false,
    },
  },
  components: {},
  created() {
    this.getInit()
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    },
  },
  data: function () {
    return {
      ruleForm: {
        name: undefined,
      },
      groupId: null,
      tableData: [],
      options: [],
      dialogVisible: this.show,
      rules: {
        name: [
          {
            required: true,
            message: this.$t('common.enter', [this.$t('customer.groupName')]),
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    handleClick(row) {
      console.log(row)
    },
    getInit() {
      // getAction({}, 'EmailConfigGroup/GetGroupList')
      //   .then((res) => {
      //     this.options = res.data
      //   })
    },
    handleClose() {
      this.resetForm('ruleForm')
      this.$emit('update:show', false)
    },
    submitAddGroup(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          postAction(this.ruleForm, 'EmailConfigGroup/Create').then((res) => {
            if (res.code === 20000) {
              this.$message({
                message: this.$t('email.createGroupSuccessfully'),
                type: 'success',
              })
              this.ruleForm = {
                name: undefined,
              }
              this.handleClose(false)
              this.$emit('closeAction')
            } else {
              this.$message({
                message: res.msg,
                type: 'warning',
              })
            }
          })
        } else {
          return false
        }
      })
      // if (this.name == null) {
      //   this.$message({
      //     type: 'info',
      //     message: '请选择分组后提交'
      //   })
      //   return false
      // }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style scoped></style>
